package com.bbbk.util.word.conf;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
@Configuration
public class MyInitializingBean implements InitializingBean {

	@Autowired
    private RequestMappingHandlerAdapter adapter;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		
		List<HandlerMethodReturnValueHandler> returnValueHandlers = adapter.getReturnValueHandlers();
		
		returnValueHandlers = new ArrayList<>(returnValueHandlers);
		
		for (int i=0;i<returnValueHandlers.size();i++) {
			HandlerMethodReturnValueHandler requestResponseBodyMethodProcessor = returnValueHandlers.get(i);
			if(requestResponseBodyMethodProcessor instanceof RequestResponseBodyMethodProcessor) {
				RequestResponseBodyMethodProcessor returnValueHandler = (RequestResponseBodyMethodProcessor)requestResponseBodyMethodProcessor;
				MyRequestResponseBodyMethodProcessor myProcessor = new MyRequestResponseBodyMethodProcessor(returnValueHandler);
				
				returnValueHandlers.add(i, myProcessor);
				returnValueHandlers.remove(i+1);
				break;
				
			}
		}
		adapter.setReturnValueHandlers(returnValueHandlers);
	}


}
